package org.jivesoftware.openfire.chatlogs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



import org.jivesoftware.database.DbConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;







public class DbChatLogsManager {
	private DbChatLogsManager() {
    }
	public static DbChatLogsManager getInstance() {
	         return ChatLogsManager;		
	}
	
	
	private static final Logger Log = LoggerFactory.getLogger(DbChatLogsManager.class);
	
    private static final DbChatLogsManager ChatLogsManager = new DbChatLogsManager();
	
	private static final String INSERTLOGS = "INSERT INTO ofChatLogs(messageId, msgUser,sessionJID, sender, receiver, createDate, length, content, detail) VALUES(?,?,?,?,?,?,?,?,?)";
	private static final String QUERYLOGS = "SELECT messageId,  sender, receiver, createDate, content FROM ofChatLogs where (msgUser = ? and sender = ? and receiver = ?) or ( msgUser = ? and sender = ? and receiver = ?)";
	private static final String DELETEWHOLOGS = "DELETE FROM ofChatLogs where msgUser = ?";
	public static Connection getConnection(){
		Connection con = null;
		 try {		
			 con = DbConnectionManager.getConnection();
			
		}
		 catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.debug("DriverManager.getConnection(url,user,pwd) failed");
		}
		return con;
		
	}
	public boolean add(ChatLogs logs) {
        Connection con = getConnection();
        PreparedStatement pstmt = null;
      

        try {
            pstmt = con.prepareStatement(INSERTLOGS);
            int i = 1;
            pstmt.setLong(i++, logs.getMessageId());
            pstmt.setString(i++,logs.getMsgUser());
            pstmt.setString(i++, logs.getSessionJID());
            pstmt.setString(i++, logs.getSender());
            pstmt.setString(i++, logs.getReceiver());
            pstmt.setString(i++, logs.getCreateDate());
            pstmt.setInt(i++, logs.getLength());
            pstmt.setString(i++, logs.getContent());
            pstmt.setString(i++, logs.getDetail());  
            return pstmt.execute();
        } catch (SQLException sqle) {
            Log.info("chatLogs add exception: {}", sqle);
            return false;
        } finally {
        		 DbConnectionManager.closeConnection(pstmt, con);
        }
    }
	public boolean  deleteWhoHistory(String who){
		Connection con = getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(DELETEWHOLOGS);
            int i = 1;
            pstmt.setString(i, who);
            return pstmt.execute();
        } catch (SQLException sqle) {
            Log.info("chatLogs delete exception: {}", sqle);
            return false;
        } finally {
        		 DbConnectionManager.closeConnection(pstmt, con);
        }
	}
	
	public List<HashMap<String,Object>> query(String sender,String receiver,String msgUser){
		Connection con = getConnection();
		PreparedStatement pstmt = null;
		List<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
		try {
			pstmt = con.prepareStatement(QUERYLOGS);
			int i = 1;
			pstmt.setString(i++,msgUser);
			 pstmt.setString(i++, sender);
	         pstmt.setString(i++, receiver);
			pstmt.setString(i++,msgUser);
	         pstmt.setString(i++, receiver);
	         pstmt.setString(i++, sender);
	         Log.info(msgUser);
	         Log.info(sender);
	         Log.info(receiver);
	         
	         ResultSet rs = pstmt.executeQuery();
             ResultSetMetaData rsmd = rs.getMetaData();
             
             int columnCount = rsmd.getColumnCount();     
             while (rs.next()) {  
                 HashMap<String, Object> map = new HashMap<String, Object>();  
               
                 for (int j = 1; j <= columnCount; ++j) {  
                     String columnVal = rs.getString(j);  
                     if (columnVal == null) {  
                         columnVal = "";  
                     }  
                     map.put(rsmd.getColumnName(j), columnVal);  
                 }  
                 result.add(map);  
             }  
             return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.info("Hashmap query failed");
			return result;
		}finally{
			 DbConnectionManager.closeConnection(pstmt, con);
		}
		 
	}
}
